package com.srcbox.file.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.srcbox.file.R
import com.srcbox.file.util.EggUtil
import kotlinx.android.synthetic.main.about.*
import kotlinx.android.synthetic.main.donation_list.view.*
import kotlinx.android.synthetic.main.main_one_list_adapter.view.*

class DonationList(val context: Context, private val list: JSONArray) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.donation_list, parent, false)
        return MeHolder(context, view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MeHolder) {
            holder.bind(list.getJSONObject(position))
        }
    }

    class MeHolder(val context: Context, itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(jsonObject: JSONObject) {
            itemView.donation_name.text = jsonObject.getString("name")
            itemView.donation_qq.text = jsonObject.getString("qq")
            itemView.donation_money.text = jsonObject.getString("money")
            itemView.donation_name.setOnClickListener {
                EggUtil.copyText(context as Activity, itemView.donation_name.text.toString())
                EggUtil.toast(context, "已复制")
            }

            itemView.donation_qq.setOnClickListener {
                EggUtil.copyText(context as Activity, itemView.donation_qq.text.toString())
                EggUtil.toast(context, "已复制")
            }

            itemView.donation_money.setOnClickListener {
                EggUtil.copyText(context as Activity, itemView.donation_money.text.toString())
                EggUtil.toast(context, "已复制")
            }
        }
    }
}